package m1;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author Amos
 * @date 2022/4/27
 */

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        // 新建消费者实例
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("c1");
        // 设置name server
        c.setNamesrvAddr("192.168.126.133:9876");
        // 设置从哪里接收消息
        c.subscribe("Topic1", "Tag1 || Tag2");
        // 添加消息处理监听器
        c.setMessageListener((MessageListenerConcurrently) (msgs, consumeConcurrentlyContext) -> {
            for (MessageExt ext : msgs) {
//                ext.getBody().toString();
                String s = new String(ext.getBody());
                System.out.println("收到：" + s);
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        // 启动
        c.start();
    }
}
